from prefect import Flow,task, Task,unmapped, Parameter
import sys
import os
from os.path import join, splitext
import matplotlib.pyplot as plt
from pandas import to_datetime
import xarray as xr
from scipy import ndimage, integrate
import plotly.graph_objects as go
import plotly plotly.tools.set_credentials_file(username='dgrimshandl', api_key='••••••••••')
# path of the rydanalysis library
sys.path.insert(0, '/home/david/Institut/data-analysis/rydanalysis/')
from rydanalysis import *
old_data_root ='/home/david/Institut/2019_IEI'
data_root ='/home/david/Institut/2019_IEI_new'
analysis_root='/home/david/Institut/analysis'
seq_path = "October/22/OmegaPScanFinalFinalFinal/"
seq = ExpSequence(join(data_root,seq_path))
%%time
ds = seq.raw_data.get_images()
ref = ds['image_03']
atoms = ds['image_01']
dark = ds['image_05']
xslice=slice(10,90)
yslice=slice(10,380)
dark = dark[:,xslice,yslice]
ref = ref[:,xslice,yslice]
std_shot = ref.std(dim=['x','y'])
mean_shot = ref.mean(dim=['x','y'])
std = ref.groupby('pABSx').std(dim='shot')
std_seq = ref.std(dim='shot')
mean_seq = ref.mean(dim='shot')
std_p = ref.groupby('pABSx').std('shot')
mean_p = ref.groupby('pABSx').mean('shot')
intens = ref.mean(('x','y'))
intens_mean = intens.groupby('pABSx').mean('shot')
intens_std = intens.groupby('pABSx').std('shot')
bg = dark.mean()
fig2 = go.FigureWidget()
fig2.add_scattergl(x=intens_mean-bg,y=intens_std,mode='markers')
fig2.update_xaxes(title_text="mean accumulated intensity [counts/pixel]");
fig2.update_yaxes(title_text="standard deviation [counts/pixel]")
intens.pABSx
fhist = go.FigureWidget()
fhist.add_histogram(x=intens,nbinsx=400)
fhist.update_xaxes(title_text = 'accumulated intensity [counts/pixel]')
fhist.update_yaxes(title_text = '# shots')
intens
fig3 = go.FigureWidget()
fig3.add_scattergl(x=intens.timestamp,y=intens,mode='markers',marker=dict(color=intens.pABSx,colorscale='Viridis'))
tm = intens.timestamp
from datetime import datetime
import plotly
plotly.__version__
ratio = (std_p/np.sqrt((mean_p-dark.mean())))
fim = go.FigureWidget()
fim.add_heatmapgl(z=ratio[0])
fim.data[0].z = dark[200]
fig = go.FigureWidget()
fig.add_scattergl(x=mean_shot,y=std_shot,mode='markers');
fig.add_scattergl(x=mean_shot,y=std_shot,mode='markers');
fig.add_scattergl(x=mean_shot,y=std_shot,mode='markers')
fig.update_layout(xaxis_type="log",
yaxis_type="log")
fig.update_xaxes(title_text="mean intensity [counts]");
fig.update_yaxes(title_text="standard deviation [counts]");
fig.data[0].y= std_p[:,52,185]
fig.data[0].x= mean_p[:,52,185]-bg
fig.data[0].name='single pixel'
fig.data[1].x= np.linspace(fig.data[2].x.min(),fig.data[2].x.max(),10)
fig.data[1].y= np.sqrt(np.linspace(max(fig.data[2].x.min(),0),fig.data[2].x.max(),10))
fig.data[1].mode='lines'
fig.data[1].name='$\sqrt{}$'
#fig.add_scatter(x=std_p[:,52,185],y=std_p[:,52,185])
fig.data[2].x=mean_p.mean(('x','y'))-bg
fig.data[2].y=std_p.mean(('x','y'))
fig.data[2].mode='lines+markers'
fig.data[2].name='average over all pixels'
binned_by_intensity = ref.groupby_bins(ref.mean(dim=('x','y')), bins=500)
binned_by_intensity
std_bin = binned_by_intensity.std(dim='shot')
mean_bin = binned_by_intensity.mean(dim='shot')
std_bin = std_bin.dropna(dim=std_bin.dims[0])
mean_bin = mean_bin.dropna(dim=mean_bin.dims[0])
std_bin_mean_im=std_bin.mean(dim=('x','y'))
mean_bin_mean_im=mean_bin.mean(dim=('x','y'))
fim.data[0].z =std_p/np.sqrt(std_bin[90])
fhist = go.FigureWidget()
fhist.add_histogram(x=mean_shot)
f3 = go.FigureWidget()
f3.add_scatter(x=mean_bin_mean_im,y=std_bin_mean_im,mode='markers')